<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
  <head>
    <meta charset="UTF-8">
    <title>授权页面-Long-admin</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link  th:href="@{/X-admin/css/font.css}" rel="stylesheet" type="text/css">
    <link  th:href="@{/X-admin/css/xadmin.css}" rel="stylesheet" type="text/css">
  </head>
  <body>
    <div class="x-nav">
      <input id="getValue" type="hidden"/>
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">UPMS</a>
        <a>
          <cite>授权</cite></a>
      </span>

      <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">&#x1002;</i></a>
    </div>
    <div class="x-body">
      <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so">
          <input type="text" name="name"  placeholder="请输入角色名" autocomplete="off" class="layui-input">
          <button class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
        </form>
      </div>
      <div  class="layui-btn-group demoTable">
        <button class="layui-btn" data-type="getCheckData">授权</button>
      </div>
      <table class="layui-table" id="roleList" lay-filter="roleList">

      </table>

    </div>

    <script type="text/javascript"  th:attr="src=@{/X-admin/lib/jquery-3.2.1.min.js}"></script>
    <script type="text/javascript"  th:attr="src=@{/X-admin/lib/layui/layui.js}"></script>
    <script type="text/javascript"  th:attr="src=@{/X-admin/js/xadmin.js}"></script>
    <script type="text/javascript"  th:attr="src=@{/js/hylUtil.js}"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

  </body>
</html>
<script>
  var tableIns;
    layui.use(['table','jquery'], function(){
        var table = layui.table
        tableIns=table.render({
            elem: '#roleList'
            ,url:'/roleController/list'
            ,cellMinWidth: 80
            ,cols: [[
                {type:'numbers'}
                ,{type: 'checkbox'}
                ,{field:'id', title:'ID', width:100, unresize: true, sort: true}
                ,{field:'name', title:'角色名'}
                ,{field:'pname', title: '父节点', with:50}
                ,{field:'remark', title: '描述', with:100}
//                ,{field:'resourceNames', title:'资源名称',with:60}
            ]]
            ,even:true
            ,page: true
            ,limits: [5, 7, 10]
            ,limit: 5 //每页默认显示的数量
        });

        //监听表格复选框选择
        table.on('checkbox(roleList)', function(obj){
            console.log(obj)
        });

        var $ = layui.$, active = {
            getCheckData: function(){ //获取选中数据
                var checkStatus = table.checkStatus('roleList')
                    ,data = checkStatus.data;
                //需要授权的人的id
                var users=parent.$("#getValue").data("obj");
                var ids="";
                for (var i=0;i<users.length;i++){
                    if(ids=""){
                        ids=ids+users[i].id;
                    }else {
                        ids=ids+","+users[i].id;
                    }
                }

                //授权的权限id拼接
                var roleIds="";
                for (var j=0;j<data.length;j++){
                    if(roleIds=""){
                        roleIds=roleIds+data[j].id;
                    }else {
                        roleIds=roleIds+","+data[j].id;
                    }
                }

                $.post('userController/grant',
                    {ids:ids,roleIds:roleIds},
                    function (result) {
                        if (result.success){
                            layer.msg('授权成功');
                        }else{
                            layer.msg('授权失败');
                        }

                },
                'json');

            }
        };

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
<script type="text/javascript">
    layui.use('form', function(){
        var form = layui.form;

        //监听提交
        form.on('submit(sreach)', function(data){
            layer.msg(JSON.stringify(data.field));
            tableIns.reload({
                where:data.field
            });
            return false;
        });
    });
</script>